package com.baselib.android.utils;

import android.content.Context;
import android.graphics.Point;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.WindowManager;

public class DensityUtils {
    private static DisplayMetrics mDisplayMetrics;

    public static float getPrinterUnit() {
        return 23.622047f;
    }

    public static float dp2px(Context context, float f) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, f, context.getResources().getDisplayMetrics());
    }

    public static float sp2px(Context context, float f) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, f, context.getResources().getDisplayMetrics());
    }

    public static int px2dp(Context context, float f) {
        return (int) Math.ceil((double) (f / context.getResources().getDisplayMetrics().density));
    }

    public static int px2sp(Context context, float f) {
        return (int) Math.ceil((double) (f / context.getResources().getDisplayMetrics().scaledDensity));
    }

    public static DisplayMetrics getDisplayMetrics() {
        if (mDisplayMetrics == null) {
            mDisplayMetrics = new DisplayMetrics();
        }
        return mDisplayMetrics;
    }

    public static float getECGUnit(Context context) {
        Point point = new Point();
        WindowManager windowManager = (WindowManager) context.getSystemService("window");
        if (windowManager != null) {
            windowManager.getDefaultDisplay().getRealSize(point);
        }
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        return Math.max(displayMetrics.xdpi / 2.54f, displayMetrics.ydpi / 2.54f) / 10.0f;
    }
}
